package projet.volleystat;

import projet.volleystat.automate.StateListener;
import projet.volleystat.automate.VolleyAction;
import projet.volleystat.automate.VolleyState;
import projet.volleystat.automate.VolleyStateMachine;
import projet.volleystat.automate.VolleyStateMachineException;

/**
 * @author Lameira Nelaupe Ferrand Fiore
 * @version 1.0 Terrain contient a la fois la vue et le modele de l'application
 *          en Match. Terrain sert d'interface entre la vue et le modele, en
 *          transmettant des signaux venant du modele pour modifier la vue.
 */
public class Court {

	protected VolleyStateMachine volleyStateMachine;
	protected Team defTeam;
	protected Team oppTeam;
	protected DBInfo db;
	protected MatchActivity context;

	/**
	 * @param defTeam
	 * @param oppTeam
	 * @param teamEngaging
	 *            Initialise une simulation d'un match entre eqDef et eqAdv.
	 *            eqEngageante est l'equipe ayant le service.
	 */
	public Court(Team defTeam, Team oppTeam, Team teamEngaging, DBInfo db,
			MatchActivity cxt) {

		this.db = db;
		this.defTeam = defTeam;
		this.oppTeam = oppTeam;
		this.context = cxt;
		if (defTeam.getPlayerlist().size() < 6) {
			// Indiquez a l'entraineur que l'equipe est insuffisante
		}
		if (oppTeam.getPlayerlist().size() < 6) {
			// Indiquez a l'entraineur que l'equipe est insuffisante
		}
		// Les joueurs selectionnes sont les six premiers dans leurs ordres
		volleyStateMachine = new VolleyStateMachine(defTeam, oppTeam,
				new StateListener() {

					@Override
					public void serviceSignal(int idFault) {
						System.out.println("Service");
						context.setBrightButtonFirstPosPlayerTeam(volleyStateMachine
								.currentState().getCurrentTeam());
					}

					@Override
					public void defTeamRotationSignal() {
						System.out.println("Rotation equipe defensive");
						context.setMyPlayers(volleyStateMachine.currentState()
								.getDefTeamPosition());
					}

					@Override
					public void oppTeamRotationSignal() {
						System.out.println("Rotation equipe adverse");
						context.setAdvPlayers(volleyStateMachine.currentState()
								.getOppTeamPosition());
					}

					@Override
					public void markedPointSignal(Team team) {
						System.out.println("Point marque par " + team.getName());
						context.setMyScore(Short.toString(volleyStateMachine
								.currentState().getDefTeamScore()));
						context.setAdvScore(Short.toString(volleyStateMachine
								.currentState().getOppTeamScore()));
						context.setMyWinSets(Short.toString(volleyStateMachine
								.currentState().getDefTeamWonSetCount()));
						context.setAdvWinSets(Short.toString(volleyStateMachine
								.currentState().getOppTeamWonSetCount()));
						context.resetBrightButton();
					}

					@Override
					public void faultSignal(int idFault) {
						System.out.println("Faute");
						context.setMyScore(Short.toString(volleyStateMachine
								.currentState().getDefTeamScore()));
						context.setAdvScore(Short.toString(volleyStateMachine
								.currentState().getOppTeamScore()));
						context.setMyWinSets(Short.toString(volleyStateMachine
								.currentState().getDefTeamWonSetCount()));
						context.setAdvWinSets(Short.toString(volleyStateMachine
								.currentState().getOppTeamWonSetCount()));
						context.resetBrightButton();
					}

					@Override
					public void playerReplacingSignal(Player playerRemplace,
							Player playerRemplacing) {
						System.out.println("Remplacement de "
								+ playerRemplace.getName() + " par "
								+ playerRemplacing.getName());
						context.setMyPlayers(volleyStateMachine.currentState()
								.getDefTeamPosition());
						context.setAdvPlayers(volleyStateMachine.currentState()
								.getOppTeamPosition());
					}

					@Override
					public void matchEndingSignal(Team winningTeam) {
						System.out.println("Fin match gagne par "
								+ winningTeam.getName());
						context.matchEnding();
					}

					@Override
					public void receptionSignal(Player player) {
						System.out.println("Reception de " + player.getName());
					}

					@Override
					public void passSignal(Player player) {
						System.out.println("Passe de " + player.getName());
					}
				});
		volleyStateMachine.createFirstState(teamEngaging);
	}

	/**
	 * @param action
	 * @return EtatVolley Signale qu'une action s'est produite a l'automate.
	 *         L'automate genere alors un nouvel etat coherent EtatVolley.
	 */
	public VolleyState signalAction(VolleyAction action)
			throws VolleyStateMachineException {
		volleyStateMachine.generateState(action);
		return volleyStateMachine.currentState();
	}

	/**
	 * @throws DatabaseException
	 *             Enregistre toutes les actions du match (automate).
	 */
	public int registerStat() throws DatabaseException {
		return db.registerMatch(volleyStateMachine);
	}
}
